package huawei.js2025;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 选修课
 */
public class Question39 {

    public static class GradeInfo implements Comparable<GradeInfo> {
        private String classNo; // 班号
        private String no; // 学号

        private int grade1; // 第一门成绩
        private int grade2; // 第二门成绩
        private boolean  hasGrade1; // 是否有第一门成绩
        private boolean  hasGrade2; // 是否有第二门成绩
        private int totalScore; // 总成绩

        public String getClassNo() {
            return classNo;
        }

        public String getNo() {
            return this.no;
        }

        public GradeInfo(String no) {
            this.no = no;
            this.classNo = no.substring(0, 6);
        }
        public void setGrade1(int grade1) {
            this.grade1 = grade1;
            hasGrade1 = true;
            this.totalScore += grade1;
        }
        public void setGrade2(int grade2) {
            this.grade2 = grade2;
            hasGrade2 = true;
            this.totalScore += grade2;
        }

        @Override
        public int compareTo(GradeInfo o) {
            int ch = o.totalScore - this.totalScore;
            if (ch == 0) {
                return this.no.compareTo(o.no);
            } else {
                return ch;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String class1 = sc.nextLine(); // 第一门
        String class2 = sc.nextLine(); // 第二门

        List<GradeInfo> list = new ArrayList<>();
        for (String string : class1.split(";")) {
            String[] strings = string.split(",");
            GradeInfo gradeInfo = new GradeInfo(strings[0]);
            gradeInfo.setGrade1(Integer.parseInt(strings[1]));
            list.add(gradeInfo);
        }

        Map<String, GradeInfo> map = list.stream().collect(Collectors.toMap(GradeInfo::getNo, e -> e));

        boolean has = false;
        for (String string : class2.split(";")) {
            String[] strings = string.split(",");
            if (map.containsKey(strings[0])) {
                GradeInfo gradeInfo = map.get(strings[0]);
                gradeInfo.setGrade2(Integer.parseInt(strings[1]));
                has = true;
            }
        }

        if (!has) {
            System.out.println("NULL");
            return;
        }

        Map<String, List<GradeInfo>> groupMap = list.stream().collect(Collectors.groupingBy(GradeInfo::getClassNo));
        List<String> classNos = new ArrayList(groupMap.keySet());
        Collections.sort(classNos);

        // 班级编号小的先输出
        for (String classNo : classNos) {
            List<GradeInfo> gradeInfos = groupMap.get(classNo);
            gradeInfos = gradeInfos.stream().filter(e-> e.hasGrade1 && e.hasGrade2).collect(Collectors.toList());

            if (gradeInfos.isEmpty()) continue;

            Collections.sort(gradeInfos);

            System.out.println(classNo);
            System.out.println(gradeInfos.stream().map(GradeInfo::getNo).collect(Collectors.joining(";")));
        }
    }
}
